package com.mycat.common.util;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * property工具类
 * 
 * @author Roger
 */
public class PropertyHelper {

	private static PropertyHelper _instance;
	Properties _portalProps = new Properties();

	/**
	 * 是否存在当前key
	 * 
	 * @param key
	 *            当前key
	 * @return
	 */
	public static boolean containsKey(String key) {
		return _getInstance()._portalProps.containsKey(key);
	}

	/**
	 * 获取当前key的值
	 * 
	 * @param key
	 *            当前key
	 * @return
	 */
	public static String get(String key) {
		String value = _getInstance()._portalProps.getProperty(key);
		return value;
	}

	public static void set(String key, String value) {
		_getInstance()._portalProps.setProperty(key, value);
	}

	private static PropertyHelper _getInstance() {
		if (_instance == null) {
			synchronized (PropertyHelper.class) {
				if (_instance == null) {
					_instance = new PropertyHelper();
				}
			}
		}
		return _instance;
	}

	private PropertyHelper() {
		ClassLoader classLoader = getClass().getClassLoader();
		try {
			URL url = classLoader.getResource("fdfs_client.properties");

			if (url != null) {
				InputStream is = url.openStream();
				_portalProps.load(is);
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		System.out.println(PropertyHelper.get("fastdfs.secet_key"));
	}
}
